﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    using Galaktika.BI.Silverlight.Controls.Data;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;
    using Galaktika.BI.Runtime.Services;

    public class GetDataCommand : ServerCommand
    {
        public GetDataCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.GetDataCommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.DataSourceId = invoker[KnownInvokeArgs.DataSourceId];
            //this.Path = invoker[KnownInvokeArgs.Path];

            if (this.ContextId == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.ContextId);
            }
            if (this.DataSourceId == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.DataSourceId);
            }
            //if (this.Path == null)
            //{
            //    throw new ArgumentNullException(KnownInvokeArgs.Path);
            //}
        }

        public string ContextId { get; private set; }
        public string DataSourceId { get; private set; }
        //public string Path { get; private set; }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            DataBinder binder = DataBinder.Current;

            object obj = binder.CreateDataObject(this.ContextId, this.DataSourceId);

            res.Content = XmlUtility.Obj2XmlStr(obj, Common.Namespace);
            res.ContentType = binder.GetContentType(this.DataSourceId);

            return res;
        }
    }
}
